package common.world.layer
{
	import common.core.GSprite;
	import common.manager.GScene;
	import common.manager.ObserverEvt;
	import common.manager.Resource;
	import common.world.World;
	
	import flash.display.Bitmap;
	import flash.display.Scene;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Rectangle;

	/**
	 * 游戏世界背景地图
	 * @author liuyong
	 */	
	public class Backgound extends GSprite
	{
		public static var LOADMAPPROGRESS	: String = "BACKGROUND_LOADMAPPROGRESS";		
		public static var LOADMAPPCOMPLETE : String = "BACKGROUND_LOADMAPPCOMPLETE"; 
		
		private var m_mapId : String;
		private var m_smallMapId : String;
		
		private var mapBitmap : Bitmap;
		
		public function Backgound()
		{
			mapBitmap = new Bitmap;
			this.addChild(mapBitmap);
			
			this.addEventListener(MouseEvent.CLICK,OnClickMap);
		}
		
		private function OnClickMap(evt : MouseEvent): void
		{
			ObserverEvt.Send(World.EVENT_MOUSECLICK,evt);
		}
		
		public function destory() : void
		{
			this.removeEventListener(MouseEvent.CLICK,OnClickMap);
			
			m_mapId = null;
			m_smallMapId = null;
			mapBitmap = null;
		}
		
		/**
		 *设置背景图片地址 
		 * @param path
		 */		
		public function set url(path:String) : void
		{
			m_mapId = path;
			
			Resource.add(path,path);
			
			Resource.start(path,loadBackgroundComplete,loadBackGroundProgress);
		}
		
		public function setURL(mapURL:String,smallMapURL:String="") : void
		{
			if(smallMapURL)
			{
				m_mapId = mapURL;
				smallurl = smallMapURL;
			}
			else
			{
				url = mapURL;
			}
		}
		
		/**
		 * 背景图片加载过程
		 * @param evt  ProgressEvent事件
		 * ObserverEvt抛出加载过程进度 参数为evt : ProgressEvent
		 */		
		private function loadBackGroundProgress(evt : ProgressEvent) : void
		{
			ObserverEvt.Send(LOADMAPPROGRESS,evt);
		}
		
		/**
		 * 背景图片加载完成
		 * @param evt  Event事件
		 * ObserverEvt抛出加载完成事件 参数为evt : ProgressEvent
		 */		
		private function loadBackgroundComplete(evt : Event) : void
		{
			Resource.stop(m_mapId,loadBackgroundComplete);
			
			var map : Bitmap = Resource.getBitmap(m_mapId,true);
			
			mapBitmap.bitmapData = map.bitmapData;
			mapBitmap.width = map.width;
			mapBitmap.height = map.height;
			
			ObserverEvt.Send(LOADMAPPCOMPLETE,evt);
			
			trace("load map success");
		}
		
		
		
		
		/**
		 *设置背景小图片地址 
		 * @param path
		 */		
		private function set smallurl(path:String) : void
		{
			m_smallMapId = path;
			
			Resource.add(path,path);
			
			Resource.start(path,loadSmallBackgroundComplete,loadSmallBackGroundProgress);
		}
		
		/**
		 * 背景小图片加载过程
		 * @param evt  ProgressEvent事件
		 * ObserverEvt抛出加载过程进度 参数为evt : ProgressEvent
		 */		
		private function loadSmallBackGroundProgress(evt : ProgressEvent) : void
		{

		}
		
		/**
		 * 背景小图片加载完成
		 * @param evt  Event事件
		 * ObserverEvt抛出加载完成事件 参数为evt : ProgressEvent
		 */		
		private function loadSmallBackgroundComplete(evt : Event) : void
		{
			Resource.stop(m_smallMapId,loadBackgroundComplete);
			
			mapBitmap.bitmapData = Resource.getBitmap(m_smallMapId,true).bitmapData;
			
			mapBitmap.width = this.width;
			mapBitmap.height = this.height;
			
			mapBitmap.x = 0;
			mapBitmap.y = 0;
			
			url = m_mapId;
			
			trace("load smallmap success");
		}
	}
}